//
//  CSGXHomeSearchController.m
//  CarSalesMS_GCX
//
//  Created by Ssky on 2016/10/25.
//  Copyright © 2016年 Tony. All rights reserved.
//

#import "CSGXHomeSearchController.h"
#import "CSGXHomeSaleCategoryView.h"
#import "CSGXSearchCell.h"
#import "CSGXTimeBar.h"
#import "CSSearchHeader.h"
#import "CSHomeSearchSaleController.h"

#import "TOrderInfoDAL.h"

#import "TOrderInfoModel.h"

NSString * const searchCellId = @"searchCellId";
NSString * const searchHeaderId = @"searchHeaderId";

@interface CSGXHomeSearchController () <CSGXCategoryViewActionDelegate, UITableViewDataSource, UITableViewDelegate>


@property (nonatomic,weak) CSGXHomeSaleCategoryView * vCategory;

@property (nonatomic,weak) CSGXTimeBar * vTimeBar;

@property (nonatomic,weak) UITableView * tvContent;


///数据源
@property (nonatomic, strong) NSArray<TOrderInfoModel *> * orderListSearch;

@property (nonatomic, assign) NSInteger selectedIndex;

@end

@implementation CSGXHomeSearchController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initData];
    
    self.vCategory.segments = @[@"销售", @"处理", @"退货", @"换货", @"还货", @"要货", @"退货申请"];
    [self.vCategory setWidth:100 withIndex:6];
    
#pragma mark call back text changed
    self.vTimeBar.callBackText = ^(BOOL isLegal, NSArray<NSString *> *timeArr) {
        
        //        NSLog(@"thread: %@", [NSThread currentThread]);
        if (!isLegal) {
            //alert
            dispatch_async(dispatch_get_main_queue(), ^{
                [CSGXProgressTool showErrorWithStatus:@"开始时间不能大于结束时间！" delay:1.0];
            });
            self.orderListSearch = [NSArray<TOrderInfoModel *> array];
            [self.tvContent reloadData];
            return ;
        }
        
        //select orderList from tOrderInfo
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            
            self.orderListSearch = [self loadOrderListWithTimeBegin:timeArr[0] timeEnd:timeArr[1]];
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [self.tvContent reloadData];
                self.vCategory.bubbleDicts = @[@{@"bubbleNumber":@(self.orderListSearch.count), @"bubbleIndex":@(0), @"bubbleColor":[UIColor orangeColor]}];
            });
        });
        
    };
}

#pragma mark - actions


#pragma mark - methods

- (void)initData {
    
    self.orderListSearch = [NSArray<TOrderInfoModel *> array];
}

- (NSArray<TOrderInfoModel *> *)loadOrderListWithTimeBegin:(NSString *)timeBegin timeEnd:(NSString *)timeEnd {
    
    NSArray<TOrderInfoModel *> * orderList = [TOrderInfoDAL loadOrderListWithTimeBegin:timeBegin timeEnd:timeEnd];
    
    return orderList;
}


#pragma mark - delegate
- (void)gxCategoryView:(CSGXHomeSaleCategoryView *)categoryView withSelectedIndex:(NSInteger)selectedIdnex {
    
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.orderListSearch.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CSGXSearchCell * cell = [tableView dequeueReusableCellWithIdentifier:searchCellId forIndexPath:indexPath];
    //@{@"rowIndex":rowIndex, @"orderModelSearch":orderModelSearch}
    cell.dataDictSearch = @{@"rowIndex":@(indexPath.row), @"orderModelSearch":self.orderListSearch[indexPath.row]};
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    self.selectedIndex = indexPath.row;
    
    CSHomeSearchSaleController * searchSaleVc = [[CSHomeSearchSaleController alloc] init];
    searchSaleVc.orderDashedModel = self.orderListSearch[indexPath.row];
    [self.navigationController pushViewController:searchSaleVc animated:YES];
}


#pragma mark - setupUI

- (void)setNavBar {
    
}

- (void)setupUI {
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setNavBar];
    
    //vCategory
    CSGXHomeSaleCategoryView * vCategory = [CSGXHomeSaleCategoryView gxCategoryViewWithDelegate:self page:0];
    [self.view addSubview:vCategory];
    
    vCategory.unSelectIndex = -1;
    
    [vCategory mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.height.offset(30);
    }];
    [self.view layoutIfNeeded];
    
    //
    CSGXTimeBar * vTimeBar = [[CSGXTimeBar alloc] init];
    [self.view addSubview:vTimeBar];
    
    vTimeBar.backgroundColor = [UIColor gx_colorWithRed:240 green:240 blue:240];
    
    [vTimeBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vCategory.mas_bottom).offset(2);
        make.leading.trailing.equalTo(self.view);
        make.height.offset(30);
    }];
    
    //tvContent
    UITableView * tvContent = [[UITableView alloc] init];
    [self.view addSubview:tvContent];
    
    [tvContent registerClass:[CSGXSearchCell class] forCellReuseIdentifier:searchCellId];
    [tvContent registerClass:[CSSearchHeader class] forHeaderFooterViewReuseIdentifier:searchHeaderId];
    
    tvContent.dataSource = self;
    tvContent.delegate = self;
    
    tvContent.bounces = NO;
    
    tvContent.sectionHeaderHeight = 30;
    tvContent.rowHeight = 40;
    
    [tvContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(vTimeBar.mas_bottom).offset(2);
        make.leading.bottom.trailing.equalTo(self.view);
    }];
    
    
    //
    self.vCategory = vCategory;
    self.vTimeBar = vTimeBar;
    self.tvContent = tvContent;
}









@end
